from elftools.elf.elffile import ELFFile

def print_dynsym_offsets(filename):
    with open(filename, 'rb') as f:
        elffile = ELFFile(f)

        # 获取 dynsym 和 dynstr 段
        dynsym = elffile.get_section_by_name('.dynsym')
        dynstr = elffile.get_section_by_name('.dynstr')

        if not dynsym or not dynstr:
            print("Error: .dynsym or .dynstr section not found.")
            return

        # 遍历 dynsym 段中的每个符号，并打印其在 dynstr 段的偏移量
        for symbol in dynsym.iter_symbols():
            offset = symbol['st_name']
            symbol_name = dynstr.get_string(offset)
            print(f"Symbol: {symbol_name}, Offset in .dynstr: {offset}")

if __name__ == "__main__":
    import sys
    if len(sys.argv) != 2:
        print("Usage: python script_name.py yourfile.so")
        sys.exit(1)
    print_dynsym_offsets(sys.argv[1])
